package com.example.sun.myapplication.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sun.myapplication.R;
import com.example.sun.myapplication.bean.WeChatMessageBean;

import java.util.List;

public class WeChatAdapter extends BaseAdapter {

    Context mContext;
    List<WeChatMessageBean> mDataList;

    public WeChatAdapter(Context context, List<WeChatMessageBean> list) {
        mContext = context;
        mDataList = list;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_wechat_message, null);

            viewHolder = new ViewHolder();
            viewHolder.ivAvatar = convertView.findViewById(R.id.item_avatar);
            viewHolder.tvName = convertView.findViewById(R.id.item_name);
            viewHolder.tvLastMessage = convertView.findViewById(R.id.item_last_message);
            viewHolder.tvLastTime = convertView.findViewById(R.id.item_last_time);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        WeChatMessageBean bean = mDataList.get(position);

        viewHolder.ivAvatar.setImageResource(bean.getAvatarResId());
        viewHolder.tvName.setText(bean.getName());
        viewHolder.tvLastMessage.setText(bean.getLastMessage());
        viewHolder.tvLastTime.setText(bean.getLastTime());

        return convertView;
    }

    static class ViewHolder {
        ImageView ivAvatar;
        TextView tvName;
        TextView tvLastMessage;
        TextView tvLastTime;
    }
}
